#!/bin/bash

function buildZlib () {
    cd ${CWD}
    SCRIPT_REPO="https://gitee.com/mirrors/zlib.git"
    
    git clone $SCRIPT_REPO

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd zlib

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --static
    )
    export CROSS_PREFIX=${FFBUILD_CROSS_PREFIX}
    export CC="${FFBUILD_CROSS_PREFIX}gcc"
    export AR="${FFBUILD_CROSS_PREFIX}ar"

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install

    cd ..
    unset CROSS_PREFIX
    unset CC
    unset AR
}

function buildIconv () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/libiconv.git iconv 

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd iconv

    ./gitsub.sh pull
    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --enable-extra-encodings
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install

    cd ..
}

function buildXml2 () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/libxml2.git libxml2

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd libxml2

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --without-python
        --disable-maintainer-mode
        --with-zlib="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./autogen.sh "${myconf[@]}"
    make -j$(nproc)
    make install

    cd ..
}

function buildFftw3 () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/fftw3 fftw3

    if [ $? -eq 0 ]; then
        echo "wget success"
    else
        echo "wget failed"
        exit -1
    fi

    cd fftw3

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --enable-maintainer-mode
        --disable-shared
        --enable-static
        --disable-fortran
        --disable-doc
        --with-our-malloc
        --enable-threads
        --with-combined-threads
        --with-incoming-stack-boundary=2
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./bootstrap.sh "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildFreetype () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/freetype2.git freetype


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd freetype

    sed -i "s?https://github.com/nyorain/dlg.git?https://gitee.com/rocklakegrass/dlg.git?g" .gitmodules 

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-zlib=yes
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildFribidi () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/fribidi.git


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd fribidi

    mkdir build && cd build

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --buildtype=release
        --default-library=static
        -Dbin=false
        -Ddocs=false
        -Dtests=false
        --cross-file=${CWD}/cross.meson
    )

    meson "${myconf[@]}" ..
    ninja -j$(nproc)
    ninja install

    sed -i 's/Cflags:/Cflags: -DFRIBIDI_LIB_STATIC/' "$FFBUILD_PREFIX"/lib/pkgconfig/fribidi.pc
}

function buildGmp () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/gmp.git gmp


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd gmp

    ./.bootstrap

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --enable-maintainer-mode
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOgg () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/ogg


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd ogg

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOpenSSL () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/openssl.git


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd openssl

    local myconf=(
        threads
        zlib
        no-shared
        no-asm
        enable-camellia
        enable-ec
        enable-srp
        --prefix="$FFBUILD_PREFIX"
        --cross-compile-prefix="$FFBUILD_CROSS_PREFIX"
        mingw64
    )

    ./Configure "${myconf[@]}"

    sed -i -e "/^CFLAGS=/s|=.*|=-I$FFBUILD_PREFIX/include|" -e "/^LDFLAGS=/s|=[[:space:]]*$|=-L$FFBUILD_PREFIX/lib|" Makefile
    sed -i "s/--target=pe-x86-64/ /g" Makefile
    sed -i "s/-m64/ /g" Makefile

    make -j$(nproc)
    make install_sw
}

function buildXz () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/xz


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd xz

    ./autogen.sh --no-po4a

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-symbol-versions
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildLibpng () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/libpng.git libpng


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd libpng

    autoreconf -i

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    export CPPFLAGS="$CPPFLAGS -I$FFBUILD_PREFIX/include"
    export LDFLAGS="$LDFLAGS -L$FFBUILD_PREFIX/lib"

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildFontconfig () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/fontconfig fc


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd fc

    ./autogen.sh --noconf

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-docs
        --enable-libxml2
        --enable-iconv
        --disable-shared
        --enable-static
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildMbedtls () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/mbedtls.git mbedtls


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd mbedtls

    sed -i '41i add_definitions(-w)' CMakeLists.txt

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" \
        -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DGEN_FILES=ON \
        -DUSE_STATIC_MBEDTLS_LIBRARY=ON -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DINSTALL_MBEDTLS_HEADERS=ON \
        ..
    make -j$(nproc)
    make install
}

function buildHarfbuzz () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/harfbuzz.git


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd harfbuzz

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    export LIBS="-lpthread"

    ./autogen.sh "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildLibsamplerate () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/libsamplerate.git libsr


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd libsr

    mkdir build
    cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DBUILD_SHARED_LIBS=NO -DBUILD_TESTING=NO -DLIBSAMPLERATE_EXAMPLES=OFF -DLIBSAMPLERATE_INSTALL=YES ..
    make -j$(nproc)
    make install
}

function buildLibudfread () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/libudfread.git libudfread


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd libudfread

    ./bootstrap

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install

    ln -s libudfread.pc "$FFBUILD_PREFIX"/lib/pkgconfig/udfread.pc
}

function buildVorbis () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/vorbis.git


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd vorbis

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --disable-oggtest
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOpenCL () {
    cd ${CWD}
    mkdir opencl && cd opencl

    git clone https://gitee.com/mirrors/OpenCL-Headers.git headers


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    mkdir -p "$FFBUILD_PREFIX"/include/CL
    cp -r headers/CL/* "$FFBUILD_PREFIX"/include/CL/.

    git clone https://gitee.com/rocklakegrass/OpenCL-ICD-Loader.git loader


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd loader

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DOPENCL_ICD_LOADER_HEADERS_DIR="$FFBUILD_PREFIX"/include -DOPENCL_ICD_LOADER_BUILD_SHARED_LIBS=OFF -DOPENCL_ICD_LOADER_DISABLE_OPENCLON12=ON -DOPENCL_ICD_LOADER_PIC=ON -DOPENCL_ICD_LOADER_BUILD_TESTING=OFF -DBUILD_TESTING=OFF ..
    make -j$(nproc)
    make install

    echo "prefix=$FFBUILD_PREFIX" > OpenCL.pc
    echo "exec_prefix=\${prefix}" >> OpenCL.pc
    echo "libdir=\${exec_prefix}/lib" >> OpenCL.pc
    echo "includedir=\${prefix}/include" >> OpenCL.pc
    echo >> OpenCL.pc
    echo "Name: OpenCL" >> OpenCL.pc
    echo "Description: OpenCL ICD Loader" >> OpenCL.pc
    echo "Version: 9999" >> OpenCL.pc
    echo "Libs: -L\${libdir} -lOpenCL" >> OpenCL.pc
    echo "Cflags: -I\${includedir}" >> OpenCL.pc
    echo "Libs.private: -lole32 -lshlwapi -lcfgmgr32" >> OpenCL.pc
    

    mkdir -p "$FFBUILD_PREFIX"/lib/pkgconfig
    mv OpenCL.pc "$FFBUILD_PREFIX"/lib/pkgconfig/OpenCL.pc
}

function buildVmaf () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/vmaf.git vmaf


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi

    cd vmaf

    echo > libvmaf/tools/meson.build

    mkdir build && cd build

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --buildtype=release
        --default-library=static
        -Dbuilt_in_models=true
        -Denable_tests=false
        -Denable_docs=false
        -Denable_avx512=false
        -Denable_float=true
        --cross-file=${CWD}/cross.meson
    )

    meson "${myconf[@]}" ../libvmaf
    ninja -j"$(nproc)"
    ninja install

    sed -i 's/Libs.private:/Libs.private: -lstdc++/; t; $ a Libs.private: -lstdc++' "$FFBUILD_PREFIX"/lib/pkgconfig/libvmaf.pc
}

function buildBrotli () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/brotli.git brotli


    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd brotli

    mkdir build && cd build

    cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..
    ninja -j$(nproc)
    ninja install

    rm -r "${FFBUILD_PREFIX}"/bin
    mv "${FFBUILD_PREFIX}"/lib/libbrotlienc{-static,}.a
    mv "${FFBUILD_PREFIX}"/lib/libbrotlidec{-static,}.a
    mv "${FFBUILD_PREFIX}"/lib/libbrotlicommon{-static,}.a

    rm "${FFBUILD_PREFIX}"/lib/libbrotli*.dll.a
}

function buildAom () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/libaom.git aom 

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd aom

    mkdir cmbuild && cd cmbuild

    # Workaround broken build system
    export CFLAGS="$CFLAGS -pthread -I/opt/ffbuild/include"

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DBUILD_SHARED_LIBS=OFF -DENABLE_EXAMPLES=NO -DENABLE_TESTS=NO -DENABLE_TOOLS=NO ..
    make -j$(nproc)
    make install

    echo "Requires.private: libvmaf" >> "$FFBUILD_PREFIX/lib/pkgconfig/aom.pc"
}

function buildAvisynth () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/AviSynthPlus.git avisynth

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd avisynth

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DHEADERS_ONLY=ON ..
    make -j$(nproc)
    make VersionGen install
}

function buildDav1d () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/dav1d.git dav1d

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd dav1d

    mkdir build && cd build

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --buildtype=release
        --default-library=static
        --cross-file=${CWD}/cross.meson
    )

    meson "${myconf[@]}" ..
    ninja -j$(nproc)
    ninja install
}

function buildFdk () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/fdk-aac.git fdk

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd fdk

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --disable-example
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildFrei0r () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/frei0r.git frei0r

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd frei0r

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --enable-cpuflags
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -C include -j$(nproc)
    make -C include install
    make install-pkgconfigDATA
}

function buildGme () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/game-music-emu.git gme

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd gme
    git checkout 6cd4bdb69be304f58c9253fb08b8362f541b3b4b

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DBUILD_SHARED_LIBS=OFF -DENABLE_UBSAN=OFF ..
    make -j$(nproc)
    make install
}

function buildAss () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/libass.git ass

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd ass

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    export CFLAGS="$CFLAGS -Dread_file=libass_internal_read_file"

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildBluray () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/libbluray.git libbluray

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd libbluray

    ./bootstrap

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --disable-doxygen-doc
        --disable-doxygen-dot
        --disable-doxygen-html
        --disable-doxygen-ps
        --disable-doxygen-pdf
        --disable-examples
        --disable-bdjava-jar
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildLame () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/lame.git lame

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd lame

    autoreconf -i

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --enable-nasm
        --disable-gtktest
        --disable-cpml
        --disable-frontend
        --disable-decoder
        --host="$FFBUILD_TOOLCHAIN"
    )

    export CFLAGS="$CFLAGS -DNDEBUG"

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOpus () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/opus.git opus 

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd opus
    git checkout bce1f392353d72d77d543bb2069a044ae1045e9d

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --disable-extra-programs
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildTheora () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/theora.git theora

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd theora

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --disable-examples
        --disable-oggtest
        --disable-vorbistest
        --disable-spec
        --disable-doc
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildVpx () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/libvpx.git libvpx

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd libvpx

    local myconf=(
        --disable-shared
        --enable-static
        --enable-pic
        --disable-examples
        --disable-tools
        --disable-docs
        --enable-vp9-highbitdepth
        --prefix="$FFBUILD_PREFIX"
        --target=arm64-win64-gcc
    )

    export CROSS="$FFBUILD_CROSS_PREFIX"

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildWebp () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/libwebp.git webp

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd webp
    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --enable-libwebpmux
        --disable-libwebpextras
        --disable-libwebpdemux
        --disable-sdl
        --disable-gl
        --disable-png
        --disable-jpeg
        --disable-tiff
        --disable-gif
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOpencore () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/opencore-amr.git opencore

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd opencore

    autoreconf -i

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --enable-amrnb-encoder
        --enable-amrnb-decoder
        --disable-examples
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildOpenh264 () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/openh264.git openh264

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd openh264

    local myconf=(
        PREFIX="$FFBUILD_PREFIX"
        INCLUDE_PREFIX="$FFBUILD_PREFIX"/include/wels
        BUILDTYPE=Release
        DEBUGSYMBOLS=False
        LIBDIR_NAME=lib
        CC="$FFBUILD_CROSS_PREFIX"gcc
        CXX="$FFBUILD_CROSS_PREFIX"g++
        AR="$FFBUILD_CROSS_PREFIX"ar
        OS=mingw_nt
        ARCH=aarch64
    )

    make -j$(nproc) "${myconf[@]}" install-static
}

function buildOpenjpeg () {
    cd ${CWD}
    git clone https://gitee.com/mirrors/openjpeg.git openjpeg

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd openjpeg

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DBUILD_SHARED_LIBS=OFF -DBUILD_PKGCONFIG_FILES=ON -DBUILD_CODEC=OFF -DWITH_ASTYLE=OFF -DBUILD_TESTING=OFF ..
    make -j$(nproc)
    make install
}

function buildAribb24 () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/aribb24.git aribb24

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd aribb24

    sed -i 's/1.0.3/1.0.4/' configure.ac

    autoreconf -i

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildTwolame () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/twolame.git twolame
    cd twolame
    NOCONFIGURE=1 ./autogen.sh
    touch doc/twolame.1

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --with-pic
        --disable-shared
        --enable-static
        --disable-sndfile
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install

    sed -i 's/Cflags:/Cflags: -DLIBTWOLAME_STATIC/' "$FFBUILD_PREFIX"/lib/pkgconfig/twolame.pc
}

function buildLibrist () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/librist.git librist

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd librist

    sed -i "21i #include <sys/time.h>" ./contrib/mbedtls/library/timing.c

    mkdir build && cd build

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --buildtype=release
        --default-library=static
        -Duse_mbedtls=true
        -Dbuiltin_mbedtls=false
        -Dbuilt_tools=false
        -Dtest=false
        -Dhave_mingw_pthreads=true
        --cross-file=${CWD}/cross.meson
    )

    meson "${myconf[@]}" ..
    ninja -j"$(nproc)"
    ninja install
}

function buildShaderc () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/shaderc.git shaderc

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd shaderc

    ./utils/git-sync-deps

    mkdir build && cd build

    cmake -GNinja -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" \
        -DSHADERC_SKIP_TESTS=ON -DSHADERC_SKIP_EXAMPLES=ON -DSHADERC_SKIP_COPYRIGHT_CHECK=ON \
        -DENABLE_EXCEPTIONS=ON -DENABLE_CTEST=OFF -DENABLE_GLSLANG_BINARIES=OFF -DSPIRV_SKIP_EXECUTABLES=ON \
        -DSPIRV_TOOLS_BUILD_STATIC=ON -DBUILD_SHARED_LIBS=OFF ..
    ninja -j$(nproc)
    ninja install

    # for some reason, this does not get installed...
    cp libshaderc_util/libshaderc_util.a "$FFBUILD_PREFIX"/lib

    echo "Libs: -lstdc++" >> "$FFBUILD_PREFIX"/lib/pkgconfig/shaderc_combined.pc
    echo "Libs: -lstdc++" >> "$FFBUILD_PREFIX"/lib/pkgconfig/shaderc_static.pc

    cp "$FFBUILD_PREFIX"/lib/pkgconfig/{shaderc_combined,shaderc}.pc


    rm -r "$FFBUILD_PREFIX"/bin "$FFBUILD_PREFIX"/lib/*.dll.a

}

function buildOpenmpt () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/OpenMPT.git openmpt

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd openmpt

    local myconf=(
        PREFIX="$FFBUILD_PREFIX"
        CXXSTDLIB_PCLIBSPRIVATE="-lstdc++"
        VERBOSE=2
        STATIC_LIB=1
        SHARED_LIB=0
        DYNLINK=0
        EXAMPLES=0
        OPENMPT123=0
        IN_OPENMPT=0
        XMP_OPENMPT=0
        DEBUG=0
        OPTIMIZE=1
        TEST=0
        MODERN=1
        FORCE_DEPS=1
        NO_MINIMP3=0
        NO_ZLIB=0
        NO_OGG=0
        NO_VORBIS=0
        NO_VORBISFILE=0
        NO_MPG123=1
        NO_SDL2=1
        NO_PULSEAUDIO=1
        NO_SNDFILE=1
        NO_PORTAUDIO=1
        NO_PORTAUDIOCPP=1
        NO_FLAC=1
        CONFIG=gcc
        TOOLCHAIN_PREFIX="$FFBUILD_CROSS_PREFIX"
    )

    export CPPFLAGS="$CPPFLAGS -DMPT_WITH_MINGWSTDTHREADS"

    make -j$(nproc) "${myconf[@]}" all install
    rm -r "$FFBUILD_PREFIX"/share/doc/libopenmpt
}

function buildRubberband () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/rubberband.git rubberband

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd rubberband

    mkdir build && cd build

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        -Ddefault_library=static
        --cross-file=${CWD}/cross.meson
    )

    meson "${myconf[@]}" ..
    ninja -j$(nproc)
    ninja install

    # Fix static linking
    echo "Requires.private: fftw3 samplerate" >> "$FFBUILD_PREFIX"/lib/pkgconfig/rubberband.pc
}

function buildSDL () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/SDL.git sdl

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd sdl
    git checkout 92c71ae385d9770e7ac3db4c9a072151e923012d

    mkdir build && cd build

    local mycmake=(
        -DSDL_SHARED=OFF
        -DSDL_STATIC=ON
        -DSDL_STATIC_PIC=ON
        -DSDL_TEST=OFF
    )

    cmake -GNinja -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" "${mycmake[@]}" ..

    ninja -j$(nproc)
    ninja install

    sed -ri -e 's/\-Wl,\-\-no\-undefined.*//' \
        -e 's/ \-mwindows//g' \
        -e 's/ \-lSDL2main//g' \
        -e 's/ \-Dmain=SDL_main//g' \
        "$FFBUILD_PREFIX"/lib/pkgconfig/sdl2.pc

    sed -ri -e 's/ -lSDL2//g' \
        -e 's/Libs: /Libs: -lSDL2 /'\
        "$FFBUILD_PREFIX"/lib/pkgconfig/sdl2.pc

    sed -i "/Version:/d" "$FFBUILD_PREFIX"/lib/pkgconfig/sdl2.pc
    sed -i "10i Version: 2.0.25" "$FFBUILD_PREFIX"/lib/pkgconfig/sdl2.pc

    echo 'Requires: samplerate' >> "$FFBUILD_PREFIX"/lib/pkgconfig/sdl2.pc
}

function buildSoxr () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/soxr.git soxr 

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd soxr

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DWITH_OPENMP=ON -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF ..
    make -j$(nproc)
    make install

    echo "Libs.private: -lgomp" >> "$FFBUILD_PREFIX"/lib/pkgconfig/soxr.pc
}

function buildSrt () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/srt.git srt 

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd srt

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" \
        -DENABLE_SHARED=OFF -DENABLE_STATIC=ON -DENABLE_CXX_DEPS=ON -DUSE_STATIC_LIBSTDCXX=ON -DENABLE_ENCRYPTION=ON -DENABLE_APPS=OFF ..
    make -j$(nproc)
    make install
}

function buildSvtav1 () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/svt-av1.git svtav1

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd svtav1
    git checkout 91b94efb2809e83d9bf041d8575b32f234dfef27

    mkdir build && cd build

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DBUILD_APPS=OFF -DENABLE_AVX512=ON ..
    make -j$(nproc)
    make install
}

function buildVidstab () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/vid.stab.git vidstab

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd vidstab
    git checkout e7715fcf329573cdcff5c57d0e4a25f4c3a0cb7f

    mkdir build && cd build

    local mycmake=(
        -DBUILD_SHARED_LIBS=OFF
        -DUSE_OMP=OFF
        -DSSE2_FOUND=FALSE
    )

    cmake -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX" "${mycmake[@]}" ..
    make -j$(nproc)
    make install
}

function buildX264 () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/x264.git x264

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd x264

    local myconf=(
        --disable-cli
        --enable-static
        --enable-pic
        --disable-lavf
        --disable-swscale
        --prefix="$FFBUILD_PREFIX"
        --host="$FFBUILD_TOOLCHAIN"
        --cross-prefix="$FFBUILD_CROSS_PREFIX"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

function buildX265 () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/x265_git.git x265

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd x265
    git checkout 0b75c44c10e605fe9e9ebed58f04a46271131827

    mkdir buildd && cd buildd

    local common_config=(
        -DCMAKE_INSTALL_PREFIX="$FFBUILD_PREFIX"
        -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN"
        -DCMAKE_BUILD_TYPE=Release
        -DENABLE_SHARED=OFF
        -DENABLE_CLI=OFF
        -DCMAKE_ASM_NASM_FLAGS=-w-macro-params-legacy
    )

    cmake "${common_config[@]}" -DHIGH_BIT_DEPTH=ON  -DENABLE_HDR10_PLUS=ON -DMAIN12=ON ../source

    make -j$(nproc)
    make install
}

function buildZimg () {
    cd ${CWD}
    git clone https://gitee.com/rocklakegrass/zimg.git zimg

    if [ $? -eq 0 ]; then
        echo "clone success"
    else
        echo "clone failed"
        exit -1
    fi
    cd zimg
    
    sed "s?<Windows.h>?<windows.h>?g" -i ./src/zimg/common/arm/cpuinfo_arm.cpp

    git submodule update --init --recursive --depth=1

    ./autogen.sh

    local myconf=(
        --prefix="$FFBUILD_PREFIX"
        --disable-shared
        --enable-static
        --with-pic
        --host="$FFBUILD_TOOLCHAIN"
    )

    ./configure "${myconf[@]}"
    make -j$(nproc)
    make install
}

buildZlib
buildIconv
buildXml2
buildFftw3
buildFreetype
buildFribidi
buildGmp
buildOgg
buildOpenSSL
buildXz
buildLibpng
buildFontconfig
buildMbedtls
buildHarfbuzz
buildLibsamplerate
buildLibudfread
buildVorbis
buildOpenCL
buildVmaf
buildBrotli
buildAom
buildAvisynth
buildDav1d
buildFdk
buildFrei0r
buildGme
buildAss
buildBluray
buildLame
buildOpus
buildTheora
buildVpx
buildWebp
buildOpencore
buildOpenh264
buildOpenjpeg
buildAribb24
buildTwolame
buildLibrist
buildShaderc
buildOpenmpt
buildRubberband
buildSDL
buildSoxr
buildSrt
buildSvtav1
buildVidstab
buildX264
buildX265
buildZimg